/**
 * @author spider
 *
 * This class will hold the information about the current directory, subfolders
 * and files needed to the program initialization.
 * Critical files, libraries, components and folders must be checked here,
 * before the program needs them!
 */
package br.unb.utils;

import java.io.File;
import java.net.URL;
import resources.ResourceRoot;

public class Path {

    /*Strings with the folder names*/
    private static final String OSName;
    private static String resourceRoot;
    private static final File homeDir;
    private static final File logDir;
    private static final File xmlDir;
    private static final File clockPath;

    static {
        OSName = System.getProperty("os.name");
        /* Directories visible to the user */
        homeDir = new File(System.getProperty("user.dir"));
        logDir = new File(Path.homeDir, "log");
        xmlDir = new File(Path.homeDir, "xml");

        /* Resources that may be stored inside the jar file*/
        resourceRoot = null; //initiated below
        clockPath = Path.getClockFile();  //sets resourceRoot.
    }

    public Path() {
        Console.println("Path() <init>");
        Console.println("Home:" + homeDir + (homeDir.exists() ? "(exists)" : "(doesn't exist)"));
        Console.println("Logs:" + logDir + (logDir.exists() ? "(exists)" : "(doesn't exist)"));
        if (!logDir.exists()) {
            Console.println("Warning LogDir doesnt exist! Create it if needed");
        }
        Console.println("Xmls:" + xmlDir + (xmlDir.exists() ? "(exists)" : "(doesn't exist)"));
        Console.println("Resources:" + resourceRoot);
        Console.println("ClockFile:" + clockPath + (clockPath.exists() ? "(exists)" : "(doesn't exist)"));
    }

    /**
     * Returns the clock library path depending on operating system
     * @return path to the library on the format of the host operating system
     */
    private static File getClockFile() {
        String lib = null;
        if (OSName.toLowerCase().indexOf("windows") >= 0) {
            lib = "clock/windows/hrtimerwin.dll";
        } else if (OSName.toLowerCase().indexOf("sunos") >= 0) {
            lib = "clock/unix/libsolarisclock.so";
        } else if (OSName.toLowerCase().indexOf("linux") >= 0) {
            lib = "clock/linux/liblinuxclock.so";
        } else {
            lib = "clock/linux/liblinuxclock.so";
        }
        //Tests different paths searching for the library file
        File f = null;
        if ((f = new File(Path.getResourceStringByAddress(lib))).exists()) {
            resourceRoot = Path.getResourceStringByAddress("");
            return f;
        } else if ((f = new File(homeDir + "/src/resources/" + lib)).exists()) {
            resourceRoot = homeDir + "/src/resources/";
            return f;
        } else if ((f = new File(homeDir + "/bin/resources/" + lib)).exists()) {
            resourceRoot = homeDir + "/bin/resources/";
            return f;
        } else {
            return null;
        }
    }

    public static URL getResourceUrlByAddress(String address) {
        if (address != null) {
            URL url = ResourceRoot.class.getResource(address);
            return url;
        }
        return null;
    }

    public static String getResourceStringByAddress(String address) {
        if (address != null) {
            URL url = ResourceRoot.class.getResource(address);
            if (url != null) {
                String s = Path.convertUrl2Path(url);
                return s;
            } else {
                Console.printError("RESOURCE_NOT_FOUND:" + address);
            }
        }
        return null;
    }

    private static String convertUrl2Path(URL u) {
        if (u != null) {
            String result = u.getPath();
            String prefix = "file:" + "/"; // File.separator;
            if (result.startsWith(prefix)) {
                result = result.substring(prefix.length());
            }
            String[] tokens = result.split("%20");
            result = tokens[0];
            for (int i = 1; i < tokens.length; i++) {
                result += " ";
                result += tokens[i];
            }
            result = result.trim();
            return result;
        }
        return null;
    }

    public static File getHomeDir() {
        return homeDir;
    }

    public static File getLogDir() {
        return logDir;
    }

    public static File getXmlDir() {
        return xmlDir;
    }

    public static File getClockPath() {
        return clockPath;
    }
}
